package com.android.lovely.ui.viewmodel

import androidx.lifecycle.viewModelScope
import com.android.lovely.api.service.userService
import com.android.lovely.data.TradeType
import com.android.lovely.data.TransactionListBean
import com.android.lovely.framework.BaseLoadViewModel
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import java.util.Calendar

/**
 *
 * @author why
 * @date 2025/3/13 16:44
 */
class TransViewModel : BaseLoadViewModel<TransactionListBean>() {
    private val _update = MutableSharedFlow<Boolean>(0)
    val update: SharedFlow<Boolean> get() = _update

    var year = if (Calendar.getInstance().get(Calendar.YEAR) > 2025) {
        Calendar.getInstance().get(Calendar.YEAR).toString()
    } else {
        "2025"
    }

    var month = if (Calendar.getInstance().get(Calendar.MONTH) + 1 > 12) {
        "12"
    } else {
        (Calendar.getInstance().get(Calendar.MONTH) + 1).toString()
    }

    var type: TradeType? = TradeType.getTypeById(null)
    override suspend fun requestApi(page: Int, loadSize: Int): List<TransactionListBean> {
        return userService.getTransactionRecord(
            mapOf(
                "year" to year,
                "month" to month.trimStart('0'),
                "page" to page,
                "size" to loadSize,
                "type" to type?.typeId
            )
        )
    }

    fun updateType() {
        viewModelScope.launchSafety {
            _update.emit(true)
        }
    }
}